<?php
/*
+--------------------------------------------------------------------------
|   rongbay.com
|   =============================================
|   by RongBay Team
|   =============================================
|   Web: http://www.rongbay.com
|   Started date : 03/2008
|   Rewrite Class for rongbay.com
+---------------------------------------------------------------------------
*/

//echo ECRewrite::formatUrl("http://192.168.28.200/rongbay.com/?page=list_detail&category_id=30&amp;ebname=Thoi-trang-Nu&page_no=1");

//exit;

class ECRewrite {

	static $count = 0;
	static $page_patten;
	
	static function formatUrl($url = "", $option = array()){
	
		if(!REWRITE_ON){
			return $url;
		}
		
		if($url)
		{
			preg_match( "/page=([a-zA-Z0-9_\-]{1,32})/i", $url, $matches );

			if(isset($matches[1]) && $matches[1] )
			{
				return self::buildUrl( $matches[1] , $url);
			}
		}
		
		return $url;
	}
	
	private function buildUrl($page = "", $url = "")
	{
		if($page && $url)
		{
			$page_patten =  self::rulePage($page);
			
			if(!$page_patten)
			{
				$page_patten = array(
						'url_in' =>	array (
							 "/\?page=([a-zA-Z0-9_-]*)&/",					
							 "/\?page=([a-zA-Z0-9_-]*)/",					
							),
							
						'url_out' => array (
							 "\$1.html?",	
							 "\$1.html",	
							),
						);
			}
			
			$url = str_replace( "&amp;", '&', $url );
			
			return preg_replace($page_patten['url_in'], $page_patten['url_out'], $url);
		}
		
		return;
	}
	
	private function rulePage($page = "")
	{
		if($page)
		{
			if(!self::$page_patten)
			{
				self::$page_patten = array(
				
					'list_detail' => array (
						'url_in' =>	array (
							 "/\?page=list_detail&catid=([0-9]+)&cname=([a-zA-Z0-9_\-]*)&page_no=([0-9]+)/",	
							 "/\?page=list_detail&catid=([0-9]+)&cname=([a-zA-Z0-9_\-]*)/",	
							),
							
						'url_out' => array (
							"c\$1/\$2/page-\$3",
							"c\$1/\$2",					
							),
						),
										
					'item_detail' => array (
						'url_in' =>	array (
							 "/\?page=item_detail&id=([0-9]+)&ebname=([a-zA-Z0-9_\-]*)&nice_name=([a-zA-Z0-9_\-]+)/",	
							 "/\?page=item_detail&id=([0-9]+)&ebname=([a-zA-Z0-9_\-]*)/",
							 "/\?page=item_detail&id=([0-9]+)/",			
							),
							
						'url_out' => array (
							 "\$3/p\$1/\$2.html",
							 "p\$1/\$2.html",
							 "p\$1.html", 				
							),
						),
					'sph_search' => array (
									'url_in' =>	array (
										 "/\?page=sph_search/",					
										),
										
									'url_out' => array (
										 "search",	
										)
								),
						
				);
			}
			
			return isset(self::$page_patten[$page]) ? self::$page_patten[$page] : '';
		}
		
		return;
	}
				
	
	static function doReplace(&$string){
	
	$url_in = array(
		 "'\?page=home&'", //home
		 "'\?page=home'", //home
		 "'\?page=rss'", //Rss
		 
		 //list detail with page
		 "'\?page=list_detail&category_id=([0-9]+)&type=1&ebname=([a-zA-Z0-9_-]*)&page_no=([0-9]+)'",	//list detail
		 "'\?page=list_detail&category_id=([0-9]+)&type=2&ebname=([a-zA-Z0-9_-]*)&page_no=([0-9]+)'",	//list detail
		 "'\?page=list_detail&category_id=([0-9]+)&type=3&ebname=([a-zA-Z0-9_-]*)&page_no=([0-9]+)'",	//list detail
		 
		 "'\?page=list_detail&category_id=([0-9]+)&type=1&ebname=([a-zA-Z0-9_-]*)'",	//list detail
		 "'\?page=list_detail&category_id=([0-9]+)&type=2&ebname=([a-zA-Z0-9_-]*)'",	//list detail
		 "'\?page=list_detail&category_id=([0-9]+)&type=3&ebname=([a-zA-Z0-9_-]*)'",	//list detail
		 "'\?page=list_detail&category_id=([0-9]+)&ebname=([a-zA-Z0-9_-]*)&page_no=([0-9]+)'",	//list detail
		 "'\?page=list_detail&category_id=([0-9]+)&ebname=([a-zA-Z0-9_-]*)'",	//list detail
		 
		 "'\?page=item_detail&id=([0-9]+)&ebname=([a-zA-Z0-9_-]*)&nice_name=([a-zA-Z0-9_-]+)'",	//product detail
		 "'\?page=item_detail&id=([0-9]+)&ebname=([a-zA-Z0-9_-]*)'",							//product detail
		 "'\?page=item_detail&id=([0-9]+)'",													//product detail
		 
		 "'\?page=AllFeedback&id=([0-9]+)&category_id=([0-9]+)&ebname=([a-zA-Z0-9_-]*)'",	//view all new FeedBack
		 "'\?page=AllFeedback&id=([0-9]+)&ebname=([a-zA-Z0-9_-]*)'",						//view all new FeedBack
		 
		 "'\?page=shop&user_id=([0-9]+)&user_name=([a-zA-Z0-9_-]+)&label_id=([0-9]+)&ebname=([a-zA-Z0-9_-]*)'",//Shop
		 "'\?page=shop&user_name=([a-zA-Z0-9_-]+)&label_id=([0-9]+)&mode=shop&ebname=([a-zA-Z0-9_-]*)'",//Shop
		 "'\?page=shop&user_name=([a-zA-Z0-9_-]+)&label_id=([0-9]+)&ebname=([a-zA-Z0-9_-]*)'",//Shop
		 "'\?page=shop&user_id=([0-9]+)&user_name=([a-zA-Z0-9_-]+)'",		//Shop
		 "'\?page=shop&user_name=([a-zA-Z0-9_-]+)&mode=shop'",				//Shop
		 "'\?page=shop&user_name=([a-zA-Z0-9_-]+)&mode=comment'",			//Shop
		 "'\?page=shop&user_name=([a-zA-Z0-9_-]+)&mode=setting'",			//Shop
		 "'\?page=shop&user_name=([a-zA-Z0-9_-]+)'",						//Shop
		 
		 "'\?page=user_entry&user_name=([a-zA-Z0-9_-]*)&cmd_entry=view&entry_id=([0-9]+)&ebname=([a-zA-Z0-9_-]*)'",	//Xem Bài viết				
		 "'\?page=user_entry&user_name=([a-zA-Z0-9_-]*)'",					//Bài viết
		 "'\?page=user_entry&user_id=([0-9]+)&user_name=([a-zA-Z0-9_-]*)'", //Bài viết
		 
		//RSS
		"'ebxml\.php\?act=rss&code=latest'",
		"'ebxml\.php\?act=rss&ebname=([a-zA-Z0-9_-]*)'",
		"'ebxml\.php\?act=rss&cat_id=([0-9]+)&ebname=([a-zA-Z0-9_-]*)'",
		
		"'ebxml\.php\?act=rss&code=user&user_id=([0-9]+)&ebname=([a-zA-Z0-9_-]*)'",

	 	 //sphinx Search 
	 	 "'\?page=sph_search'",	
		 
	 	 //Tinh, Thanh pho
	 	 //"'\?province_id=([0-9]+)&ebname=([a-zA-Z0-9_-]*)'",//Chon tinh thanh
	 	 
	 	 //Else Enbac pages
	 	 "'\?page=([a-zA-Z0-9_-]*)&'",	
	 	 "'\?page=([a-zA-Z0-9_-]*)'",	
		);
		
		$url_out = array(
		 "./?",//home 
		 "./",//home
		 "rss/",//Rss
		 
		 //list detail with paging
		 "c\\1/Mua-ban/\\2/page-\\3",//.html",//list detail
		 "c\\1/Mua-ban/\\2/page-\\3",//.html",//list detail
		 "c\\1/Dau-gia/\\2/page-\\3",//.html",//list detail
		 
		 "c\\1/Mua-ban/\\2",//.html",//list detail
		 "c\\1/Mua-ban/\\2",//.html",//list detail
		 "c\\1/Dau-gia/\\2",//.html",//list detail
		 "c\\1/\\2/page-\\3",//.html",//list detail
		 "c\\1/\\2",//.html",//list detail
		 
		 //product detail
		 "\\3/p\\1/\\2.html",
		 "p\\1/\\2.html",
		 "p\\1.html", 
		 
		 //viewl all new feedback		 
		 "c\\2/fb\\1/\\3.html",
		 "fb\\1/\\2.html",
		 
		 //shop
		 "Shop/\\1/L\\3/\\4",//.html",
		 "Shop/\\1/L\\2/\\3",//.html",
		 "Shop/\\1/L\\2/\\3",//.html",
		 "Shop\\1/\\2",//.html",
		 "Shop/\\1/San-pham",//.html",
		 "Shop/\\1/Luu-but",//.html",
		 "Shop/\\1/Cai-dat",//.html",
		 "\\1",//"Shop/\\1",//.html",
		 
		 //Bài viết
		 "Bai-viet/\\1/e\\2/\\3",//.html",
		 "Bai-viet/\\1",//.html",//"Bai-viet/\\1/\\2.html",
		 "Bai-viet/\\2",//.html",
		 
		 //RSS
		 "rss/moinhat.rss",
		 "\\1.rss",
		 "rss/c\\1_\\2.rss",
		
		 "rss/u\\1/\\2.rss",
		
	 	 //sphinx search
	 	 "search",
		 
	 	 //Tinh, Thanh pho
		 //"tp\\1/\\2.html",
		 
		 //Else Enbac pages	 
		 "\\1.html?",
		 "\\1.html",
		);
			
		self::$count++;//Đếm số lần hàm đc dùng
		
		return preg_replace($url_in, $url_out, $string);
		//if(!DEBUG){
			/*$url_in [] = '![\t]*[\r\n]+[\t]*!';
			$url_out[] = ' ';*/
			
			/*$url_in [] = '!\>[\s]+|[\s]+\>!';
			$url_out[] = '>';
			
			$url_in [] = '![\s]+\<!';
			$url_out[] = '<';*/
			
			/*$url_in [] = '/((?<!\?>)\n)[\s]+/m';
			$url_out[] = '\1';*/
		//}
		//return preg_replace("[[:space:]]+", " ",preg_replace($url_in, $url_out, $string));
		//return preg_replace($url_in, $url_out, $string);
	    //return self::make_one_line(preg_replace($url_in, $url_out, $string));
	}
	/*	
	static function make_one_line($html = ""){
		if($html!="" && !DEBUG){
			//str_replace(array(chr(13),chr(11),chr(10),chr(9),"&nbsp;")," ",$html)
        
	        $search  = array(
	        				'![\t]*[\r\n]+[\t]*!',
	        				//'!\>[\s]+|[\s]+\>!',
	        				//'![\s]+\<!',
	        										);
			$replace = array(
								' ',
							//	'>',
							//	'<',
									);
									
			$html = preg_replace($search, $replace, $html);
			$html = preg_replace("[[:space:]]+", " ", $html);
			
			$html = trim(preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $html));
		}

		return $html;
		
		//$html = trim(preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $html));
		//return preg_replace('!\s+!', ' ', $html);
	}*/
}
//End class
?>